package com.liush.bookstore.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liush.bookstore.config.Result;
import com.liush.bookstore.mapper.UserMapper;
import com.liush.bookstore.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/*
 * 用户信息控制层
 * */
@Controller
@Slf4j
public class UserController {

    @Autowired
    private UserMapper userMapper;

    //访问登录页面
    @RequestMapping("/user/login")
    public String login() {
        return "/user/login.html";
    }

    //用户登录
    @RequestMapping("/user/check")
    @ResponseBody
    public Object checkUser(@RequestParam(name = "username", required = true) String username,
                            @RequestParam(name = "password", required = true) String password,
                            HttpServletRequest request) {
        //根据账号密码查询数据库
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUname, username);
        queryWrapper.eq(User::getPwd, password);
        User user = userMapper.selectOne(queryWrapper);
        request.getSession().setAttribute("user", user);
        //判断查询的用户是否为空，不为空则登录成功
        if (user == null || user.equals("")) {
            return Result.error("账号或密码不对");
        }
        return Result.success("登录成功");
    }

    //用户退出登录
    @RequestMapping("/user/logout")
    public String logout(HttpServletRequest request) {
        //删除user作用域
        request.removeAttribute("user");
        //强制让当前session失效
        request.getSession().invalidate();
        return "redirect:/index";
    }

    //访问注册页面
    @RequestMapping("/user/regist")
    public String regist() {
        return "/user/regist.html";
    }

    //注册新用户
    @RequestMapping("/user/add")
    @ResponseBody
    public Object addUser(@RequestParam(name = "uname", required = true) String uname,
                          @RequestParam(name = "pwd", required = true) String pwd,
                          @RequestParam(name = "email", required = true) String email,
                          @RequestParam(name = "yourCode", required = true) String yourCode,
                          HttpServletRequest request) {
        //校验验证是否正确
        String code = (String) request.getSession().getAttribute("code");
        if (!code.equals(yourCode)) {
            return Result.error("验证码识别错误！请重新输入！");
        }
        //检验用户是否已注册
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUname, uname);
        User one = userMapper.selectOne(queryWrapper);
        if (one == null) {
            //注册新用户，用户权限管理role默认为0
            User user = new User();
            user.setUname(uname);
            user.setPwd(pwd);
            user.setEmail(email);
            user.setRole(0);
            int num = userMapper.insert(user);
            if (num != 1) {
                return Result.error("注册失败！请稍后再试！");
            }
            return Result.success("注册成功，请您登录！");
        } else {
            return Result.error("该用户已注册！请重新注册！");
        }
    }
}
